package cn.woblog.recyclerviewsample.divider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.support.v7.widget.RecyclerView.State;
import android.util.Log;
import android.view.View;
import cn.woblog.recyclerviewsample.R;

/**
 * Created by Pich on 17/4/5.
 */

public class CustomDividerItemDecoration extends RecyclerView.ItemDecoration {

  private final int orientation;
  private final Context context;
  private Drawable divider;

  public CustomDividerItemDecoration(Context context, int orientation) {
    this.context = context;
    this.orientation = orientation;
//    divider=context.getResources().getDrawable(R.drawable.divider);
    divider = context.getResources().getDrawable(R.drawable.item_divider);
  }

  @Override
  public void onDraw(Canvas c, RecyclerView parent, State state) {
    super.onDraw(c, parent, state);
    if (orientation == LinearLayoutCompat.HORIZONTAL) {
      drawHorizontal(c, parent, state);
    } else {
      drawVertical(c, parent, state);
    }
  }

  private void drawHorizontal(Canvas c, RecyclerView parent, State state) {
//    int childCount = parent.getChildCount() - 1;
    int childCount = parent.getChildCount();
    int left = 0;
    int top = parent.getPaddingTop();
    int right = 0;
    int bottom = parent.getHeight() - parent.getPaddingBottom();
    for (int i = 0; i < childCount; i++) {
      View view = parent.getChildAt(i);
      RecyclerView.LayoutParams params = (LayoutParams) view.getLayoutParams(); //考虑，padding
      left = view.getRight();
      right = left + divider.getIntrinsicHeight(); //我们在自定义drawable的是是，写死了高度，所以只能用高度
      divider.setBounds(left, top, right, bottom);
      divider.draw(c);
  }
  }

  /**
   * 思路：就是获取每个item，计算divider的left,top,right,bottom
   */
  private void drawVertical(Canvas c, RecyclerView parent, State state) {
//    int childCount = parent.getChildCount() - 1;
    int childCount = parent.getChildCount();
    int left = parent.getPaddingLeft();
    int top = 0;
    int right = parent.getWidth() - parent.getPaddingRight(); //考虑右边的padding
    int bottom = 0;
    for (int i = 0; i < childCount; i++) { //不绘制最后一个条目的分割线
      View view = parent.getChildAt(i);
      RecyclerView.LayoutParams params = (LayoutParams) view.getLayoutParams(); //考虑，padding
      top = view.getBottom() + params.topMargin; //就是当前view底部到顶部的距离
      bottom = top + divider.getIntrinsicHeight() - params.bottomMargin; //就是top+divider高度
      divider.setBounds(left, top, right, bottom);
      divider.draw(c);
    }
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
    int childAdapterPosition = parent.getChildAdapterPosition(view);

//    int lastCount = parent.getAdapter().getItemCount() - 1;
    int lastCount = parent.getAdapter().getItemCount();

    //如果当前条目与是最后一个条目，就不设置divider padding
//    if (childAdapterPosition == lastCount) {
//      outRect.set(0, 0, 0, 0);
//      return;
//    }
    if (childAdapterPosition == 0) {
      outRect.set(0, 0, divider.getIntrinsicHeight(), divider.getIntrinsicHeight());
    } else {
      Log.d("TAG", childAdapterPosition + "," + lastCount);
      if (orientation == LinearLayoutCompat.HORIZONTAL) {
        outRect.set(0, 0, divider.getIntrinsicHeight(), 0); //0,0,30,0，设置宽度
      } else {
        outRect.set(0, 0, 0, divider.getIntrinsicHeight()); //0,0,0,30，设置高度
      }
    }


  }


}
